package com.duan.laugh.compute.controller;

import com.duan.laugh.compute.pojo.entity.Compute;
import com.duan.laugh.compute.service.ComputeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

@RestController
@RequestMapping("compute")
public class ComputeController {
    List<Compute> list = new ArrayList<>();

    public ComputeController(){
        for (long i = 1; i <= 10000; i++) {
            Compute compute = new Compute();
            compute.setId(i);
            BigDecimal decimal = BigDecimal.TEN;
            compute.setV1(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV2(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV3(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV4(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV5(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV6(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV7(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV8(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV9(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV10(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV11(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV12(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV13(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV14(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV15(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV16(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV17(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV18(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV19(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV20(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV21(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV22(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV23(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            compute.setV24(decimal.multiply(BigDecimal.valueOf(new Random().nextInt(1000))));
            list.add(compute);
        }
        System.out.println("组装数据完毕");

    }
    @Autowired
    private ComputeService computeService;
    @GetMapping("list")
    public List<Compute> list(){
        long startTime = System.currentTimeMillis();

        List<Compute> list = computeService.list();
        long endTime = System.currentTimeMillis();
        System.out.println("用时：" + (endTime - startTime));
        return list;
    }
    @RequestMapping
    public void test(){
        computeService.test();
    }

    @GetMapping("updateV1")
    public boolean updateV1(){
        long startTime = System.currentTimeMillis();
        computeService.updateV1();
        long endTime = System.currentTimeMillis();
        System.out.println("用时：" + (endTime - startTime));
        return true;
    }
    @GetMapping("updateListV1")
    public boolean updateListV1(){
        List<Compute> list = new ArrayList<>();
        for (long i = 1; i <= 10000; i++) {
            Compute compute = new Compute();
            compute.setId(i);
            compute.setV1(BigDecimal.TEN);
            list.add(compute);
        }
        System.out.println("组装数据完毕");
        long startTime = System.currentTimeMillis();
        int count = computeService.updateListV1(list);
        long endTime = System.currentTimeMillis();
        System.out.println("用时：" + (endTime - startTime) + "，更新条数：" + count);
        return true;
    }
    @GetMapping("updateList")
    public Integer updateList(){
        System.out.println("开始批量更新" + list.size() + "条");
        long startTime = System.currentTimeMillis();
        int count = computeService.updateList(list);
        long endTime = System.currentTimeMillis();
        System.out.println("用时：" + (endTime - startTime) + "，更新条数：" + count);
        return count;
    }
    @GetMapping("insertList")
    public boolean insertList(){
        for (int i = 0; i < 3; i++) {
            System.out.println("开始批量插入" + list.size() + "条");
            long startTime = System.currentTimeMillis();
            computeService.insertList(list);
            long endTime = System.currentTimeMillis();
            System.out.println("用时：" + (endTime - startTime));
        }

        for (int i = 0; i < 3; i++) {
            System.out.println("开始批量异步插入" + list.size() + "条");
            long startTime = System.currentTimeMillis();
            computeService.insertListParallel(list);
            long endTime = System.currentTimeMillis();
            System.out.println("用时：" + (endTime - startTime));
        }
        return true;
    }

    @GetMapping("selectByIds")
    public List<Compute> selectByIds(){
        final List<Long> collect = list.stream().map(Compute::getId).collect(Collectors.toList());
        System.out.println("开始根据id批量查询" + collect.size() + "条");
        long startTime = System.currentTimeMillis();
        final List<Compute> computes = computeService.selectByIds(collect);
        long endTime = System.currentTimeMillis();
        System.out.println("selectByIds用时：" + (endTime - startTime));
        return computes;
    }

    @GetMapping("selectMapByIds")
    public List<Map<String, Object>> selectMapByIds(){
        final List<Long> collect = list.stream().map(Compute::getId).collect(Collectors.toList());
        System.out.println("开始根据id批量查询，返回map" + collect.size() + "条");
        long startTime = System.currentTimeMillis();
        final List<Map<String, Object>> computes = computeService.selectMapByIds(collect);
        long endTime = System.currentTimeMillis();
        System.out.println("selectMapByIds用时：" + (endTime - startTime));
        return computes;
    }

    @GetMapping("insertByV1")
    public List<Map<String, Object>> insertByV1(){
        final List<Long> collect = list.stream().map(Compute::getId).collect(Collectors.toList());
        System.out.println("开始插入id，" + collect.size() + "条");
        long startTime = System.currentTimeMillis();
        final List<Map<String, Object>> maps = computeService.insertByV1(collect);
        long endTime = System.currentTimeMillis();
        System.out.println("insertByV1用时：" + (endTime - startTime));
        return maps;
    }
}
